<template>
  <div>
        <el-form :model="queryParams" ref="queryForm" :inline="true"  label-width="68px" @submit.native.prevent>
          <el-form-item label="指标名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="精准匹配"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="selectIndicator"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="selectIndicator">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
             @click="topAdd"
            >新增</el-button>
          </el-col>
        </el-row>
      <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="indicatorList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :header-cell-style="getRowClass"
    >
      <el-table-column prop="name" label="指标名称" width="260"></el-table-column>
      <el-table-column prop="id" label="编号" width="100"></el-table-column>
      <el-table-column prop="isLeafNode" :formatter="isLeafNodeFormat"  label="叶子节点" width="100"></el-table-column>
      <el-table-column prop="weight" label="权重" width="100"></el-table-column>
      <el-table-column prop="sort" label="排序" width="200"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="200">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button v-if="scope.row.isLeafNode == 1"
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
          >新增</el-button>
           <el-button v-if="scope.row.isLeafNode == 0"
            size="mini"
            type="text"
            icon="el-icon-guide"
            @click="handleOptions(scope.row)"
          >选项</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
        

   <!-- 添加或修改指标对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级指标" prop="parentName">
              <el-input v-model="form.parentName" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="指标名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入指标名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="指标权重" prop="name">
              <el-input-number v-model="form.weight" controls-position="right" :precision="2" :step="0.01" :min="0" :max="1"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="叶子节点">
              <el-select v-model="form.isLeafNode"   placeholder="请选择">
                <el-option
                  v-for="dict in isLeafNodeOptions"
                  :key="dict.id"
                  :label="dict.format"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="sort">
              <el-input-number v-model="form.sort" controls-position="right" :min="1" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="indicatorName" :visible.sync="openOptions" width="1000px" append-to-body>
           <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="topAddOptions"
            >新增指标</el-button>
          </el-col>
          <el-table v-loading="loading" :data="optionsList">
            <el-table-column label="编号" align="center" key="id" prop="id"  />
            <el-table-column label="名称" align="center" key="name" prop="name" />
            <el-table-column label="描述" align="center" key="content" prop="content"  />
            <el-table-column label="权重" align="center" key="weight" prop="weight"  />
            <el-table-column label="排序" align="center" key="sort" prop="sort"  />
            <el-table-column label="创建时间" align="center" prop="createTime" width="160">
            </el-table-column>
            <el-table-column 
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
          <template slot-scope="scope">
            <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateOptions(scope.row)"
          >修改</el-button>
           <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDeleteOptions(scope.row)"
          >删除</el-button>
            </template>
          </el-table-column>
          </el-table>
    </el-dialog>

     <!-- 添加或修改指标对话框 -->
    <el-dialog :title="titleOptions" :visible.sync="openAddOptions" width="680px" append-to-body>
      <el-form ref="optionsForm" :model="optionsForm" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="选项名称" prop="name">
              <el-input v-model="optionsForm.name" placeholder="请输入选项名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="选项权重" prop="weight">
              <el-input-number v-model="optionsForm.weight" controls-position="right" :step="1" :min="0" :max="100" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="显示排序" prop="sort">
              <el-input-number v-model="optionsForm.sort" controls-position="right" :min="1" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="选项描述" prop="content">
              <el-input type="textarea"
                :rows="4" 
                v-model="optionsForm.content" 
                placeholder="请输入选项描述" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormOptions">确 定</el-button>
        <el-button @click="cancelOptions">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getIsLeafNodeApi} from "@/api/system/common"
import {selectIndicatorApi,deleteIndicatorApi,addIndicatorApi,updateIndicatorApi,selectIndicatorOptionsApi,addIndicatorOptionsApi,deleteIndicatorOptionsApi,updateIndicatorOptionsApi} from "@/api/system/indicator/info"
export default {
  name: "indicator",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      refreshTable: true,
      // 表格数据
      indicatorList: [],
      optionsList:[],
      indicatorName: '指标名字',
      options:{},
      optionsForm:{},
      //叶子节点选项
      isLeafNodeOptions:[],
      //  // 指标树选项
      // menuOptions: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
       // 弹出层标题
      title: "",
      titleOptions:"",
      // 是否显示弹出层
      open: false,
      openOptions: false,
      openAddOptions: false,
      form: {},
      // 查询参数
      queryParams: {
        name: undefined
      },
      // 列信息
      columns: [
       
      ],
      // 表单校验
      rules: {
        
      }
    };
  },
  watch: {
   
  },
  created() {
    getIsLeafNodeApi().then(data=>{
      this.isLeafNodeOptions = data.data;
    })
    this.selectIndicator();
  },
  methods: {
    getRowClass ({ rowIndex }) {
      if (rowIndex == 0) {
        return 'background:#afdfe4'
      } else {
        return ''
      }
    },
    isLeafNodeFormat(row, column) {
      if (row.isLeafNode =='0') {
        return '是'
      } else  {
        return '否'
      } 
  },
    //查询
    selectIndicator(){
      selectIndicatorApi(this.queryParams).then(data=>{
        this.indicatorList=  data.data;
        this.loading = false;
      }).catch(()=>{
        this.loading = false;
      })
    },
    /** 新增按钮操作 */
    topAdd() {
      this.reset();
      this.form.parentId = 0;
      this.form.parentName = "主类目";
      this.form.parentIds = "0";
      this.title = "添加指标";
      this.open = true;
    },
    topAddOptions(){
      this.resetOptions();
      this.titleOptions = "添加指标选项";
      this.optionsForm.indCode = this.options.id;
      this.openAddOptions = true;
    },
    resetOptions(){
      this.optionsForm = {
        id: undefined,
        indCode:undefined,
        name:undefined,
        weight:undefined,
        sort:undefined,
        content:undefined
      };
       if (this.$refs["optionsForm"]) {
		    this.$refs["optionsForm"].resetFields();
	    }
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.form.parentId = row.id;
      this.form.parentName = row.name;
      this.form.parentIds = row.parentIds;
      this.title = "添加指标";
      this.open = true;
    },
     /** 选项操作 */
    handleOptions(row) {
      this.options.id = row.id;
      this.indicatorName = "当前指标——"+row.name;
      this.selectIndicatorOptions(row.id);
      this.openOptions = true;
    },
    selectIndicatorOptions(id){
       selectIndicatorOptionsApi({'id':id}).then(data=>{
        this.optionsList = data.data;
      }).catch(()=>{
        this.roleList = null;
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      if (this.$refs["queryForm"]) {
		    this.$refs["queryForm"].resetFields();
	    }
      this.selectIndicator();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除指标编号为"' + row.id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=> {
          deleteIndicatorApi({
            "id": row.id
          }).then(data=>{
            this.selectIndicator();
            this.$message({
               type: 'success',
              message: '删除成功'
            });
          })
        })
    },
    //删除指标选项
    handleDeleteOptions(row){
      this.$confirm('是否确认删除指标选项编号为"' + row.id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=> {
          deleteIndicatorOptionsApi({
            "indicatorOptionsId": row.id
          }).then(data=>{
            this.selectIndicatorOptions(this.options.id);
            this.$message({
               type: 'success',
              message: '删除成功'
            });
          })
        })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
        this.form.id = row.id;
        this.form.sort = row.sort;
        this.form.parentName = row.parentName;
        this.form.isLeafNode = row.isLeafNode;
        this.form.weight = row.weight;
        if(row.parentId=="0"){
          this.form.parentName = "主类目";
        }
        this.form.name = row.name;
        this.title = "修改指标";
        this.open = true;  
    }, 
     /** 选项修改操作 */
    handleUpdateOptions(row) {
       this.resetOptions();
       this.optionsForm.id = row.id;
       this.optionsForm.sort = row.sort;
       this.optionsForm.name = row.name;
       this.optionsForm.weight = row.weight;
       this.optionsForm.content = row.content;
       this.titleOptions = "修改指标选项";
       this.openAddOptions = true;  
    }, 
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        parentId: undefined,
        isLeafNode:undefined,
        sort: undefined,
        weight: undefined,
        parentName: undefined 
      };
       if (this.$refs["form"]) {
		    this.$refs["form"].resetFields();
	    }
    },
     /** 提交按钮 */
    submitFormOptions: function() {
      
          if (this.optionsForm.id != undefined) {
            updateIndicatorOptionsApi(this.optionsForm).then(data=>{
              this.openAddOptions = false;
              this.selectIndicatorOptions(this.options.id);
              this.resetOptions();
              this.$message({
                type: 'success',
                message: '修改成功'
              });
            })
          } else {
            addIndicatorOptionsApi(this.optionsForm).then(data=>{
              this.openAddOptions = false;
              this.selectIndicatorOptions(this.options.id);
              this.resetOptions();
              this.$message({
                type: 'success',
                message: '添加成功'
              });
            })
          }
     
    }, 
     /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateIndicatorApi(this.form).then(data=>{
              this.open = false;
              this.selectIndicator();
              this.reset();
              this.$message({
                type: 'success',
                message: '修改成功'
              });
            })
          } else {
            addIndicatorApi(this.form).then(data=>{
              this.open = false;
              this.selectIndicator();
              this.reset();
              this.$message({
                type: 'success',
                message: '添加成功'
              });
            })
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    cancelOptions(){
      this.openAddOptions = false;
    }
  }
};
</script>